﻿import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Basic
import FilUI

TextField {
    id: control

    property bool disabled                      : false
    property int iconSource                     : 0
    property color normalColor                  : Qt.rgba(27/255, 27/255, 27/255, 1)
    property color disableColor                 : Qt.rgba(160/255, 160/255, 160/255, 1)
    property color placeholderNormalColor       : Qt.rgba(96/255, 96/255, 96/255, 1)
    property color placeholderFocusColor        : Qt.rgba(141/255, 141/255, 141/255, 1)
    property color placeholderDisableColor      : Qt.rgba(160/255, 160/255, 160/255, 1)
    property bool cleanEnabled                  : true
    property alias cleanButton                  : id_filiconbutton__clean

    signal commit(string text)

    padding                                     : 7
    leftPadding                                 : padding + 4
    enabled                                     : !disabled
    color                                       : enabled ? normalColor : disableColor
    font                                        : FilTextStyle.Body
    renderType                                  : Text.QtRendering
    selectionColor                              : FilTools.withOpacity(FilTheme.primaryColor, 0.5)
    selectedTextColor                           : control.color
    placeholderTextColor                        : {
        if (!enabled) {
            return placeholderDisableColor
        }
        if (focus) {
            return placeholderFocusColor
        }
        return placeholderNormalColor
    }
    selectByMouse                               : true
    rightPadding                                : {
        var w = 30
        if (control.cleanEnabled === false) w = 0
        if (control.readOnly)               w = 0
        return icon_end.visible ? w + 36 : w + 10
    }
    width                                       : 240
    background                                  : FilTextBoxBackground {
        inputItem                               : control
    }
    Keys.onEnterPressed                         : (event) => d.handleCommit(event)
    Keys.onReturnPressed                        : (event) => d.handleCommit(event)
    QtObject {
        id: d
        function handleCommit(event) {
            control.commit(control.text)
        }
    }
    MouseArea {
        anchors.fill                            : parent
        cursorShape                             : Qt.IBeamCursor
        acceptedButtons                         : Qt.RightButton
        onClicked                               : {
            if (control.echoMode === TextInput.Password) {
                return
            }
            if (control.readOnly && control.text === "") {
                return
            }
            menu_loader.popup()
        }
    }
    RowLayout {
        height                                  : parent.height
        anchors {
            right                               : parent.right
            rightMargin                         : 5
        }
        spacing                                 : 4
        FilIconButton {
            id: id_filiconbutton__clean
            iconSource                          : FilIcons.Cancel
            iconSize                            : 12
            Layout.preferredWidth               : 30
            Layout.preferredHeight              : 20
            Layout.alignment                    : Qt.AlignVCenter
            iconColor                           : Qt.rgba(97/255, 97/255, 97/255, 1)
            verticalPadding                     : 0
            horizontalPadding                   : 0
            visible                             : {
                if (control.cleanEnabled === false) {
                    return false
                }
                if (control.readOnly)
                    return false
                return control.text !== ""
            }
            contentDescription                  : "Clean"
            onClicked                           : {
                control.clear()
            }
        }
        FilIcon {
            id: icon_end
            iconSource                          : control.iconSource
            iconSize                            : 12
            Layout.alignment                    : Qt.AlignVCenter
            Layout.rightMargin                  : 7
            iconColor                           : Qt.rgba(97/255, 97/255, 97/255, 1)
            visible                             : control.iconSource != 0
        }
    }
    Loader {
        id: menu_loader
        Component.onDestruction                 : sourceComponent = undefined
        function popup() {
            sourceComponent = menu
        }
    }
    Component {
        id: menu
        FilTextBoxMenu {
            inputItem                           : control
            Component.onCompleted               : {
                popup()
            }
            onClosed                            : {
                menu_loader.sourceComponent = undefined
            }
        }
    }
}
